package com.hq.fxh.CustomerView.PopupWindow;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.hq.fxh.R;
import com.hq.fxh.Utils.LogUtils;
import com.hq.fxh.Utils.Tool;

import java.util.Timer;
import java.util.TimerTask;


/**
 * Created by Administrator on 2017/2/16.
 */
public class PopupForEditShare extends PopupWindow implements View.OnClickListener {


    protected View rootView;
    protected TextView tvPlateEditorBack;
    protected TextView textToolBarTitle;
    protected TextView tvPlateEditorPublish;
    protected EditText editPopupShareMoney;
    private Activity context;
    private View popView;
    ShareMoneyCallBack shareMoneyCallBack;

    public PopupForEditShare(Activity context, ShareMoneyCallBack commentCallBack) {
        super(context);
        this.context = context;
        this.shareMoneyCallBack = commentCallBack;
        popView = LayoutInflater.from(context).inflate(R.layout.popup_edit_sharemoney, null);
        initView(popView);
        initData();

    }


    public void openKeyboard() {

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);

            }
        }, 300);
    }

    public void initData() {

        //设置PopupWindow的View
        this.setContentView(popView);
        //设置PopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置PopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置PopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setAnimationStyle(R.style.dialog_anim_style);
        this.setOutsideTouchable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xffffffff);
    /*    openKeyboard();*/

        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                Tool.setAlpha(context, 1f);

            }
        });


    }


    private void initView(View rootView) {
        tvPlateEditorBack = (TextView) rootView.findViewById(R.id.tv_PlateEditor_Back);
        tvPlateEditorBack.setOnClickListener(PopupForEditShare.this);
        textToolBarTitle = (TextView) rootView.findViewById(R.id.textToolBar_Title);
        tvPlateEditorPublish = (TextView) rootView.findViewById(R.id.tv_PlateEditor_Publish);
        tvPlateEditorPublish.setOnClickListener(PopupForEditShare.this);
        editPopupShareMoney = (EditText) rootView.findViewById(R.id.edit_PopupShare_Money);

    }


    private void changeText(TextView textView) {
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.tv_PlateEditor_Back) {
            dismiss();

        } else if (view.getId() == R.id.tv_PlateEditor_Publish) {
            Editable text = editPopupShareMoney.getText();

            if (Tool.editIsEmpty(editPopupShareMoney)) {
                LogUtils.startToash(context, "请输入金额");

            } else {
                shareMoneyCallBack.callSure(String.valueOf(editPopupShareMoney.getText()));
                dismiss();

            }

        }
    }


    public interface ShareMoneyCallBack {
        void callSure(String comment);


    }


}
